<template>
  <div class="container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <!-- 聊天内容区域 -->
    <div
      class="chat-list"
      ref="myList"
    >
      <div
        class="chat-item"
        :class="item.name==='xz'?'left':'right'"
        v-for="(item,index) in list"
        :key="index"
      >
        <van-image
          fit="cover"
          round
          v-if="item.name==='xz'"
          :src="xzTx"
        />
        <div class="chat-pao">{{item.msg}}</div>
        <van-image
          fit="cover"
          round
          v-if="item.name==='self'"
          :src="photo"
        />
      </div>

    </div>
    <div class="reply-container van-hairline--top">
      <van-field
        v-model="value"
        placeholder="说点什么..."
      >
        <span
          @click="send()"
          slot="button"
          style="font-size:12px;color:#999"
        >提交</span>
      </van-field>
    </div>
  </div>
</template>

<script>

import xzTx from '@/assets/xz.png'
import io from 'socket.io-client'
import { mapGetters } from 'vuex'
export default {
  name: 'user-chat',
  data () {
    return {
      xzTx,
      value: '',
      list: []
    }
  },
  computed: {
    ...mapGetters(['photo', 'name'])
  },
  created () { // 进入组件直接跟服务器交互
    // io方法 第一个传后台接口，第二个对象 内传配置
    this.io = io('http://localhost:3000/', {
      // 因为新版本socket.io默认是socket模式，存在跨域问题。这里使用websocket模式，需要连接时指定！！！
      transports: ['websocket'],
      query: { // 令牌
        token: 'Bear urbbrzroug'
      }
    })
    // 如果跟服务器连接成功 服务器回默认回应  用 on方法可以接收到 第一个值传'connect' 固定语法参数  第二个值传回调函数
    this.io.on('connect', () => {
      this.list.push({ name: 'xz', msg: `你好 ${this.name} ` })
    })
    // 接收信息 第一个值 跟后台一致 第二个值传回调函数 参数是后台返回的数据
    this.io.on('message', (data) => {
      // console.log(data)
      this.list.push({ name: 'xz', msg: data.msg })
      this.goBottom()
    })
  },
  beforeDestroy () { // 销毁钩子函数
    // 组件销毁时，把跟服务器的交互也断开连接
    this.io.close()
  },
  methods: {
    send () { // 点击确认给后台发送消息
      // emit方法向后台发送信息
      this.io.emit('message', { name: this.name, msg: this.value })
      this.list.push({ name: 'self', msg: this.value })
      this.value = ''
    },
    goBottom () { // 自动滚轮
      this.$nextTick(() => {
        this.$refs.myList.scrollTop = this.$refs.myList.scrollHeight
      })
    }
  }

}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
